from sqlalchemy import Column, Integer, String, Enum as SqlEnum, ForeignKey
from database import Base

from sqlalchemy.orm import relationship
from models.department_model import Department
import enum


class Gender(enum.Enum):
    FEMALE = 'FEMALE'
    MALE = 'MALE'

    def to_str(self):
        return self.name
    @staticmethod
    def from_str(s):
        return Gender[s] if s in Gender.__members__ else None

#定义一下这个booktime类
class BookTime(enum.Enum):
    AM = "AM"
    PM = "PM"

    def to_str(self):
        return self.value  # 返回枚举成员的值

    @staticmethod
    def from_str(s):
        return BookTime[s] if s in BookTime.__members__ else None

    


class UserType(enum.Enum):
    PATIENT='PATIENT'
    DOCTOR='DOCTOR'
    PHARMACIST='PHARMACIST'
    WORKER='WORKER'
    ADMIN='ADMIN'

    def to_str(self):
        return self.name
    @staticmethod
    def from_str(s):
        return UserType[s] if s in UserType.__members__ else None



class Account(Base):
    __tablename__="account"
    ID = Column(Integer, primary_key=True, index=True,autoincrement=True)
    username = Column(String(50), unique=True, nullable=False)
    password_hash=Column(String(60), nullable=False)
    # password = Column(String(255), nullable=False)
    user_type=Column(SqlEnum(UserType), default=UserType.PATIENT, nullable=False)
    user_ref_id=Column(Integer,nullable=False)

    def to_dict(self):
        return {
            "ID": self.ID,
            "username": self.username,
            # "password": self.password,
            "user_type": self.user_type.to_str(),
            "user_ref_id": self.user_ref_id
        }



class Patient(Base):
    __tablename__ = "patient"

    ID = Column(Integer, primary_key=True, index=True,autoincrement=True)
    name = Column(String(50))
    gender = Column(SqlEnum(Gender),default=Gender.FEMALE,nullable=False)
    age = Column(Integer)
    phone_number = Column(String(20))

    # 添加与诊断表的反向关系
    consultations = relationship("Consultation", back_populates="patient")
    #处方表的联系
    prescriptions = relationship("Prescription", back_populates="patient")
    # 添加与预约表的反向关系
    bookings = relationship("Booking", back_populates="patient")
     # 添加与药品发放记录的关系
    medicine_records = relationship("RecordOfMedicine", back_populates="patient")
    #添加与支付表的关系
    # 添加与 Payment 的关系
    payments = relationship("Payment", back_populates="patient")

    def to_dict(self):
        return {
            "ID": self.ID,
            "name": self.name,
            "gender": self.gender.to_str(),
            "age": self.age,
            "phone_number": self.phone_number
        }

    @classmethod
    def from_dict(cls,data:dict)->"Patient":
        valid_keys = set(cls.__table__.columns.keys())
        return  cls(**{k: v for k, v in data.items() if k in valid_keys})



class Doctor(Base):
    __tablename__ = "doctor"

    ID=Column(Integer,primary_key=True,index=True,autoincrement=True)
    name=Column(String(50))
    age=Column(Integer)
    phone_number=Column(String(20))
    gender=Column(SqlEnum(Gender),default=Gender.FEMALE,nullable=False)
    title=Column(String(30))
    position=Column(String(30))
    department=Column(String(50),ForeignKey("department.name"))
    specialty=Column(String(50))
    image_url=Column(String(255))

     # 添加反向关系（类似外键的联系？）诊断表
    consultations = relationship("Consultation", back_populates="doctor")
    #处方表
    prescriptions = relationship("Prescription", back_populates="doctor")

    # 作为外键
    bookings = relationship("Booking", back_populates="doctor")

    departments = relationship("Department", back_populates="doctors")

    def to_dict(self):
        return {
            "ID": self.ID,
            "name": self.name,
            "age": self.age,
            "phone_number": self.phone_number,
            "gender": self.gender.to_str(),
            "title": self.title,
            "position": self.position,
            "department": self.department,
            "specialty": self.specialty,
            "image_url": self.image_url
        }

    @classmethod
    def from_dict(cls,data:dict)->"Doctor":
        valid_keys = set(cls.__table__.columns.keys())
        return cls(**{k: v for k,v in data.items() if k in valid_keys})






class Pharmacist(Base):
    __tablename__ = "pharmacist"

    ID = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(50))
    gender = Column(SqlEnum(Gender), default=Gender.FEMALE, nullable=False)
    age = Column(Integer)

    def to_dict(self):
        return {
            "ID": self.ID,
            "name": self.name,
            "gender": self.gender.to_str(),
            "age": self.age,
        }

    @classmethod
    def from_dict(cls, data: dict) -> "Pharmacist":
        valid_keys = set(cls.__table__.columns.keys())
        return cls(**{k: v for k, v in data.items() if k in valid_keys})
    
    # 添加与药品发放记录的关系
    dispensing_records = relationship("RecordOfMedicine", back_populates="pharmacist")
    


class Worker(Base):
    __tablename__ = "workers"

    ID = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(50))
    gender = Column(SqlEnum(Gender), default=Gender.FEMALE, nullable=False)
    phone_number=Column(String(20))
    age = Column(Integer)
    department=Column(String(30))

    def to_dict(self):
        return {
            "ID": self.ID,
            "name": self.name,
            "age": self.age,
            "gender": self.gender.to_str(),
            "phone_number": self.phone_number,
            "department": self.department,
        }

    @classmethod
    def from_dict(cls, data: dict) -> "Worker":
        valid_keys = set(cls.__table__.columns.keys())
        return cls(**{k: v for k, v in data.items() if k in valid_keys})
    
class Admin(Base):
    __tablename__ = "admin"

    ID = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(50))
    gender = Column(SqlEnum(Gender), default=Gender.FEMALE, nullable=False)
    age = Column(Integer)

    def to_dict(self):
        return {
            "ID": self.ID,
            "name": self.name,
            "gender": self.gender.to_str(),
            "age": self.age,
        }

    @classmethod
    def from_dict(cls, data: dict) -> "Admin":
        valid_keys = set(cls.__table__.columns.keys())
        return cls(**{k: v for k, v in data.items() if k in valid_keys})



